// pages/changebuy/changebuy.js
import {
  getOrderInfo,
  getMyCardGoods,
  // changeOrderGoods,
  getVipGoodsCatNum
} from '../../apis/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vegetableList: [],
    meetList: [],
    eggList: [],
    riceList: [],
    fishList: [],
    bestList: [],
    cornList: [],
    fruitList: [],
    birdList: [],
    meetliveList: [],
    freeList: [],
    allList: [],
    cardId: 0,
    submitId: 0,
    cardList: [],
    orderId: 0,
    cardTypeId: 0,
    addressId: 0,
    inventors: [],
    showModalStatus: false,
    upperLimit: [],
    card_type: '',
    single:''
  },
  // 卡类型id判断
  selectVipType: async function (that) {
    let that_id = that
    this.setData({
      cardId: that_id
    })
    const upperLimitMiddle = []
    const req = await getVipGoodsCatNum({cardType: this.data.card_type})
    // console.log('req',req);
    if (!!req) {
      for (let i = 0; i < req.data.length; i++) {
        upperLimitMiddle.push(req.data[i].upperLimit)
      }
      this.setData({
        upperLimit: upperLimitMiddle
      })
    }
    const res = await getMyCardGoods({
      cardId: that_id
    })
    const request = await getOrderInfo({
      id: this.data.orderId
    })
    this.setData({
      addressId: request.data[0].orders.addressId
    })
    const mList = []
    const vList = []
    const eList = []
    const fList = []
    const bList = []
    const rList = []
    const cList = []
    const ffList = []
    const bbList = []
    const mmList = []
    const fffList = []
    console.log(res.data)
    for (let i = 0; i < res.data.length; i++) {
      // if (!res.data[i].temporaryNum) res.data[i].temporaryNum = 0
      if (!res.data[i].buyNum) res.data[i].buyNum = 0
      if (res.data[i].totalInventory !== 0) {
      if (res.data[i].dishType === '肉') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        mList.push(res.data[i])
      }
      if (res.data[i].dishType === '蛋') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        eList.push(res.data[i])
      }
      if (res.data[i].dishType === '蔬菜') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        vList.push(res.data[i])
      }
      if (res.data[i].dishType === '大米') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        rList.push(res.data[i])
      }
      if (res.data[i].dishType === '水产') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        fList.push(res.data[i])
      }
      if (res.data[i].dishType === '特产') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        bList.push(res.data[i])
      }
      if (res.data[i].dishType === '玉米') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        cList.push(res.data[i])
      }
      if (res.data[i].dishType === '水果') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        ffList.push(res.data[i])
      }
      if (res.data[i].dishType === '禽类') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        bbList.push(res.data[i])
      }
      if (res.data[i].dishType === '畜肉') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        mmList.push(res.data[i])
      }
      if (res.data[i].dishType === '赠品') {
        for (let j = 0; j < request.data[0].ordersDetailed.length; j++) {
          if (request.data[0].ordersDetailed[j].did === res.data[i].dishId) {
            res.data[i].buyNum = request.data[0].ordersDetailed[j].quantity
          }
        }
        fffList.push(res.data[i])
      }
      }
    }
    this.setData({
      vegetableList: vList,
      meetList: mList,
      eggList: eList,
      fishList: fList,
      bestList: bList,
      riceList: rList,
      cornList: cList,
      fruitList: ffList,
      birdList: bbList,
      meetliveList: mmList,
      freeList: fffList
    })
  },
  // 子传父数据
  myevent(e) {
    if (e.detail.goodC === '蔬菜类') {
      this.setData({
        vegetableList: e.detail.good
      })
    }
    if (e.detail.goodC === '肉类') {
      this.setData({
        meetList: e.detail.good
      })
    }
    if (e.detail.goodC === '蛋类') {
      this.setData({
        eggList: e.detail.good
      })
    }
    if (e.detail.goodC === '大米类') {
      this.setData({
        riceList: e.detail.good
      })
    }
    if (e.detail.goodC === '水产类') {
      this.setData({
        fishList: e.detail.good
      })
    }
    if (e.detail.goodC === '特产类') {
      this.setData({
        bestList: e.detail.good
      })
    }
    if (e.detail.goodC === '玉米类') {
      this.setData({
        cornList: e.detail.good
      })
    }
    if (e.detail.goodC === '水果类') {
      this.setData({
        fruitList: e.detail.good
      })
    }
    if (e.detail.goodC === '禽类') {
      this.setData({
        birdList: e.detail.good
      })
    }
    if (e.detail.goodC === '畜肉类') {
      this.setData({
        meetliveList: e.detail.good
      })
    }
    if (e.detail.goodC === '赠品') {
      this.setData({
        freeList: e.detail.good
      })
    }
    // this.changeListToOne()
  },

  toToolOne(list) {
    const toolList = []
    for (let i = 0; i < list.length; i++) {
      if (list[i].buyNum !== 0) toolList.push(list[i])
    }
    return toolList
  },
  // 将选择的菜品集成
  changeListToOne() {
    const vTool = this.toToolOne(this.data.vegetableList)
    const mTool = this.toToolOne(this.data.meetList)
    const eTool = this.toToolOne(this.data.eggList)
    const rTool = this.toToolOne(this.data.riceList)
    const fTool = this.toToolOne(this.data.fishList)
    const bTool = this.toToolOne(this.data.bestList)
    const cTool = this.toToolOne(this.data.cornList)
    const ffTool = this.toToolOne(this.data.fruitList)
    const bbTool = this.toToolOne(this.data.birdList)
    const mmTool = this.toToolOne(this.data.meetliveList)
    const fffTool = this.toToolOne(this.data.freeList)
    this.setData({
      allList: [...vTool, ...mTool, ...eTool, ...rTool, ...fTool, ...bTool, ...cTool, ...ffTool, ...bbTool, ...mmTool, ...fffTool]
    })
  },
  // 获取需要提交的商品信息
  getGoodsToSubmit: function () {
    const list = [...this.data.allList]
    for (let i = 0; i < list.length; i++) {
      list[i].dishesMainId = list[i].dishId
      list[i].quantity = list[i].buyNum
      list[i].dishesMainName = list[i].dishName
    }
    this.setData({
      inventors: list
    })
  },
  // 修改菜品菜单
  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.changeListToOne()
    this.getGoodsToSubmit()
    this.util(currentStatu)
  },
  // 修改菜品
  powerDrawerClick: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    const _this = this
    // wx.showModal({
    //   title: '提示',
    //   content: '所有订单可修改且仅可修改一次，是否继续？',
    //   success: function (res) {
    //     if (res.confirm) {
          const submitForm = {
            oid: parseInt(_this.data.orderId),
            addressId: parseInt(_this.data.addressId),
            inventors: _this.data.inventors,
            // cardType: this.data.cardId
          }
          console.log(submitForm)
          const list = JSON.stringify(_this.data.allList)
          const card_ids = _this.data.submitId
          const card_types = _this.data.card_type
          const form = JSON.stringify(submitForm)
          wx.navigateTo({
            // url: `/pages/tobuy/tobuy?list=${list}&card=${card_ids}&cardtype=${card_types}&single=${this.data.single}`
            url: `/pages/tobuy/tobuy?form=${form}`
          })
          // changeOrderGoods(submitForm).then(res => {
          //   if (res.code !== 200) {
          //     return wx.showToast({
          //       title: '修改订单失败',
          //       icon: 'error',
          //       duration: 2000
          //     })
          //   }
          //   setTimeout(() => {
          //     wx.switchTab({
          //       url: '/pages/order/order',
          //     })
          //   }, 1000)
          //   wx.showToast({
          //     title: '修改订单成功',
          //     icon: 'success',
          //     duration: 1000
          //   })
          //   _this.util(currentStatu)
          // })
    //     }
    //   }
    // })
  },
  util: function (currentStatu) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    });
    this.animation = animation;
    animation.opacity(0).rotateX(-100).step();
    this.setData({
      animationData: animation.export()
    })
    setTimeout(function () {
      animation.opacity(1).rotateX(0).step();
      this.setData({
        animationData: animation
      })
      if (currentStatu == "close") {
        this.setData({
          showModalStatus: false
        });
      }
    }.bind(this), 200)
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    console.log(options)
    this.setData({
      orderId: parseInt(options.oid),
      card_type: options.type,
      single:options.single
    })
    await this.selectVipType(options.cid)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    await this.selectVipType(this.data.cardId)
  }
})